﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using BrainTechLLC;
using BrainTechLLC.WPF;
using ImageAppDataModels;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace FindDuplicates.Plugin
{
    public class CompareWindowHandler : WindowHandlerBase<CompareImagesWindow, Window>, ICompareWindowHandler
    {
        public event EventHandler Closed;
        public static PluginDefinitionLoaded<ICompareWindowHandler> Handler;
        public static readonly DuplicatesCommand DuplicatesCommand = new DuplicatesCommand();
        public static readonly AllDuplicatesCommand AllDuplicatesCommand = new AllDuplicatesCommand();
        public static readonly NotDuplicatesCommand NotDuplicatesCommand = new NotDuplicatesCommand();

        protected DupeRunner _dupeRunner = new DupeRunner();
        protected string _originalTitle;

        public CompareWindowHandler(Window1 parentWindow, PluginDefinitionLoaded<ICompareWindowHandler> handler)
            : base(parentWindow)
        {
            Handler = handler;
            _parentWindow = parentWindow;
        }

        public override void OpenWindow(Window owner)
        {
            if (_window == null)
            {
                _window = new CompareImagesWindow();
                _window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                _window.CommandBindings.Add(new CommandBinding(DuplicatesCommand, Duplicates));
                _window.CommandBindings.Add(new CommandBinding(AllDuplicatesCommand, AllDuplicates));
                _window.CommandBindings.Add(new CommandBinding(NotDuplicatesCommand, NotDuplicates));
                _window.ClosedWindow += new EventHandler<ObjectEventArgs<FileImageViewModel, FileImageViewModel>>(_window_ClosedWindow);
            }

            Window1 window = _parentWindow as Window1;
            if (window._AllFileImageCollectionViewModels != null)
            {
                for (int n = 0; n < window._AllFileImageCollectionViewModels.Count; n++)
                {
                    var collection = window._AllFileImageCollectionViewModels[n];
                    collection.FindDuplicates(new Action<List<Pair<IFileImage, IFileImage>>>((result) => FindCompleted(result)),
                        s => window.Dispatcher.BeginInvoke(new VoidDelegate(() => window.StatusMenuItem.Header = s)));
                }
            }

            window.Status.Add("Removing Duplicates");
            _originalTitle = Window1.MainWindow.Title;
            Window1.MainWindow.Title = "Beginning Search for Duplicates";
        }

        private void FindCompleted(List<Pair<IFileImage, IFileImage>> results)
        {
            Application.Current.Dispatcher.RunOnDispatcher(new OneParamDelegate((o) =>
                {
                    Window1.MainWindow.Status.Remove("Removing Duplicates");
                    List<Pair<IFileImage, IFileImage>> dupes = o as List<Pair<IFileImage, IFileImage>>;
                    results.RemoveAll(p1 => p1.obj1.FileName == p1.obj2.FileName);
                    _dupeRunner.AssignDuplicates(results);
                    System.Windows.MessageBox.Show("Found " + _dupeRunner.DupeCount.ToString() + " duplicates");

                    _dupeRunner.ShowDupe(_window);
                    Window1.MainWindow.Title = _originalTitle;
                }),
                System.Windows.Threading.DispatcherPriority.Normal,
                results);
        }

        private void Duplicates(object sender, ExecutedRoutedEventArgs e)
        {
            Window1 window = _parentWindow as Window1;
            if (window._AllFileImageCollectionViewModels != null)
            {
                for (int n = 0; n < window._AllFileImageCollectionViewModels.Count; n++)
                {
                    var collection = window._AllFileImageCollectionViewModels[n];
                    _dupeRunner.RemoveDupe(collection);
                }
            }
            _dupeRunner.ShowNextDupe(_window);
        }

        private void AllDuplicates(object sender, ExecutedRoutedEventArgs e)
        {
            Window1 window = _parentWindow as Window1;
            if (window._AllFileImageCollectionViewModels != null)
            {
                for (int n = 0; n < window._AllFileImageCollectionViewModels.Count; n++)
                {
                    var collection = window._AllFileImageCollectionViewModels[n];
                    _dupeRunner.RemoveAll(collection);
                }
            }
            _window.Close();
        }

        private void NotDuplicates(object sender, ExecutedRoutedEventArgs e)
        {
            _dupeRunner.ShowNextDupe(_window);
        }

        void _window_ClosedWindow(object sender, ObjectEventArgs<FileImageViewModel, FileImageViewModel> e)
        {
            Closed.RaiseEvent(this);
            _window = null;
            _dupeRunner.Clear();
        }
    }
}
